//
//  TTUserModel.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/6/15.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "JPUSHService.h"
#import "TTUDIDManager.h"

#import "TTUserModel.h"

@interface TTUserModel ()

@end

@implementation TTUserModel

+ (TTUserModel *)sharedObject {
    static TTUserModel *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TTUserModel new];
        shareInstance.installModel = [[TTInstallModel alloc] init];
    });
    return shareInstance;
}

- (void)initialization {
        
    //normal
    
    [self.userModelDefault setValue:@"" forKey:@"TT_token"];
    [self.userModelDefault setValue:@"" forKey:@"TT_isNewer"];

    [self.userModelDefault setValue:@"" forKey:@"TT_username"];
    [self.userModelDefault setValue:@"" forKey:@"TT_identifier"];
    [self.userModelDefault setValue:@"" forKey:@"TT_userId"];
    [self.userModelDefault setValue:@"" forKey:@"TT_nickname"];
    [self.userModelDefault setValue:@"" forKey:@"TT_realName"];

    [self.userModelDefault setValue:@"" forKey:@"TT_myCode"];
    
    [self.userModelDefault setValue:@"" forKey:@"TT_wechatNo"];
    [self.userModelDefault setValue:@"" forKey:@"TT_realName"];
    
    //IM
    [self.userModelDefault setValue:@"" forKey:@"TT_identifier"];
    [self.userModelDefault setValue:@"" forKey:@"TT_userSig"];

    
    [self.userModelDefault synchronize];
}

- (void)fillUserModelWithDic:(NSMutableDictionary *)userDic {
    if (IsStrNotEmpty([userDic valueForKey:@"username"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"username"] forKey:@"TT_username"];
        [self.userModelDefault setValue:[userDic valueForKey:@"username"] forKey:@"TT_identifier"];
    }
    if (IsStrNotEmpty([userDic valueForKey:@"imId"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"imId"] forKey:@"TT_userSig"];
    }
    if (IsStrNotEmpty([userDic valueForKey:@"userId"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"userId"] forKey:@"TT_userId"];
    }
    if (IsStrNotEmpty([userDic valueForKey:@"nickname"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"nickname"] forKey:@"TT_nickname"];
    }
    if (IsStrNotEmpty([userDic valueForKey:@"myCode"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"myCode"] forKey:@"TT_myCode"];
    }
    if (IsStrNotEmpty([userDic valueForKey:@"imgUrl"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"imgUrl"] forKey:@"TT_userIcon"];
    }
    if (IsNotNilOrNotNull([userDic valueForKey:@"sex"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"sex"] forKey:@"TT_sex"];
    }
    if (IsStrNotEmpty([userDic valueForKey:@"marryTime"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"marryTime"] forKey:@"TT_marryTime"];
    }
    // 下面信息可能是空的哦
    if (IsStrNotEmpty([userDic valueForKey:@"autograph"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"autograph"] forKey:@"TT_autograph"];
    }
    //支付宝信息
    if (IsStrNotEmpty([userDic valueForKey:@"alipayId"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"alipayId"] forKey:@"TT_alipayId"];
    }
    //支付宝信息
    if (IsStrNotEmpty([userDic valueForKey:@"realName"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"realName"] forKey:@"TT_realName"];
    }
    //微信id
    if (IsStrNotEmpty([userDic valueForKey:@"wechatNo"])) {
        [self.userModelDefault setValue:[userDic valueForKey:@"wechatNo"] forKey:@"TT_wechatNo"];
    }
    [self.userModelDefault synchronize];
    
    //绑定alias
    NSString *alias = [NSString stringWithFormat:@"%@%@",[userDic valueForKey:@"username"],[TTUDIDManager getUniqueDeviceID]];
    [JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
    } seq:1];

    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BA_LoginSuccess" object:nil userInfo:nil];
    
    
    [[StatisticManager shareManager] updateOnlineTimeWith:[Utilities getCurrentServerTimestamp] Type:BA_UpdateTimeType_loginAccount];

    //弹出信息采集页面，内部有完整判断
    

}

- (void)logOut {
    
    [[StatisticManager shareManager] updateOnlineTimeWith:[Utilities getCurrentServerTimestamp] Type:BA_UpdateTimeType_logoutAccount];

    [self initialization];
    [[TT_IMManager sharedInstance] TTIM_LogOut:^{
        [[TT_MessageManager manager] refreshUnReadCount];
    } fail:^(int code, NSString *msg) {
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BA_LogOutSuccess" object:nil userInfo:nil];

}

- (NSString *)token {
    NSString *TT_token = [self.userModelDefault valueForKey:@"TT_token"];
    return TT_token;
}

- (double)latitude {
    double TT_latitude = [self.userModelDefault doubleForKey:@"TT_latitude"];
    return TT_latitude;
}

- (double)longitude {
    double TT_longitude = [self.userModelDefault doubleForKey:@"TT_longitude"];
    return TT_longitude;
}

- (NSTimeInterval)loginTime {
    NSTimeInterval TT_loginTime = [self.userModelDefault doubleForKey:@"TT_loginTime"];
    return TT_loginTime;
}

- (NSTimeInterval)logOutTime {
    NSTimeInterval TT_logOutTime = [self.userModelDefault doubleForKey:@"TT_logOutTime"];
    return TT_logOutTime;
}

- (NSTimeInterval)openAppTime {
    NSTimeInterval TT_openAppTime = [self.userModelDefault doubleForKey:@"TT_openAppTime"];
    return TT_openAppTime;
}

- (TTUserType)isNewer {
    NSString * TT_isNewer = [self.userModelDefault valueForKey:@"TT_isNewer"];
    if (TT_isNewer.length > 0) {
        if ([TT_isNewer isEqualToString:@"1"]) {
            return TTUserType_new;
        } else {
            return TTUserType_old;
        }
    } else {
        return TTUserType_unKnow;
    }
}

- (NSString *)username {
    NSString *TT_username = [self.userModelDefault valueForKey:@"TT_username"];
    return TT_username;
}

- (NSString *)wechatNo {
    NSString *TT_wechatNo = [self.userModelDefault valueForKey:@"TT_wechatNo"];
    return TT_wechatNo;
}

- (NSString *)userIcon {
    NSString *TT_userIcon = [self.userModelDefault valueForKey:@"TT_userIcon"];
    return TT_userIcon;
}

- (NSInteger)sex {
    NSInteger TT_sex = [[self.userModelDefault valueForKey:@"TT_sex"] integerValue];
    return TT_sex;
}

- (NSString *)autograph {
    NSString *TT_autograph = [self.userModelDefault valueForKey:@"TT_autograph"];
    return TT_autograph;
}

- (NSString *)marryTime {
    NSString *TT_marryTime = [self.userModelDefault valueForKey:@"TT_marryTime"];
    return TT_marryTime;
}

- (NSString *)userId {
    NSString * TT_userId = [self.userModelDefault valueForKey:@"TT_userId"];

    return TT_userId;
}

- (NSString *)myCode {
    NSString * TT_myCode = [self.userModelDefault valueForKey:@"TT_myCode"];
    return TT_myCode;
}

- (NSString *)nickname {
    NSString *TT_nickname = [self.userModelDefault valueForKey:@"TT_nickname"];
    return TT_nickname;
}

- (NSString *)identifier {
    NSString *TT_identifier = [self.userModelDefault valueForKey:@"TT_identifier"];
    return TT_identifier;
}

- (NSString *)userSig {
    NSString *TT_userSig = [self.userModelDefault valueForKey:@"TT_userSig"];
    return TT_userSig;
}

- (NSString *)appidAt3rd {
    NSString *TT_appidAt3rd = @"1400408971";
    return TT_appidAt3rd;
}

- (NSString *)alipayId {
    NSString *TT_alipayId = [self.userModelDefault valueForKey:@"TT_alipayId"];
    return TT_alipayId;
}

- (NSString *)realName {
    NSString *TT_realName = [self.userModelDefault valueForKey:@"TT_realName"];
    return TT_realName;
}

- (NSString *)cityName {
    NSString *TT_cityName = [self.userModelDefault valueForKey:@"TT_cityName"];
    return TT_cityName;
}

- (NSString *)cityId {
    NSString *TT_cityId = [self.userModelDefault valueForKey:@"TT_cityId"];
    return TT_cityId;
}

- (NSMutableArray *)searchHistoryArray {
    NSMutableArray *searchHistoryArray = [self.userModelDefault valueForKey:@"user_searchHistoryArray"];

    return searchHistoryArray;
}

- (NSMutableArray *)imKeyWordArray {
    NSMutableArray *imKeyWordArray = [self.userModelDefault valueForKey:@"user_imKeyWordArray"];
    return imKeyWordArray;
}

- (void)addSearchHistoryWithKeyWord:(NSString *)keyWord {
    
    if ([self.searchHistoryArray containsObject:keyWord]) {
        return;
    } else {
        NSMutableArray *searchHistoryArray =[NSMutableArray arrayWithArray:self.searchHistoryArray.mutableCopy];
        [searchHistoryArray addObject:keyWord];
        [self.userModelDefault setValue:searchHistoryArray forKey:@"user_searchHistoryArray"];
        [self.userModelDefault synchronize];
    }
}

- (void)clearSearchHistory {
    [self.userModelDefault setValue:@[] forKey:@"user_searchHistoryArray"];
    [self.userModelDefault synchronize];
}

-(NSUserDefaults *)userModelDefault {
    if (!_userModelDefault) {
        _userModelDefault = [NSUserDefaults standardUserDefaults];
    }
    return _userModelDefault;
}





@end
